// pages/matching/matching.js
const app = getApp();
var util = require('../../utils/util.js')
var http = require('../../utils/http.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    touxiang: '',
    money: '',
    loginName: '',
    aiPkPlayer: '',
    aiList: [],
    maskShow: true,
    matchingShow: true,
    pkShow: false,
    point: '',
    peopleList: [
      
    ],
    addList: [
      {
        img: "https://gxxc-1258879759.cos.ap-guangzhou.myqcloud.com/tree/utils/1543911167600.jpeg",
        name: "生命短暂而孤单",
        money: 59
    },
      {
        img: "https://gxxc-1258879759.cos.ap-guangzhou.myqcloud.com/tree/utils/1543911170326.jpeg",
        name: "也许有一天会相见",
        money: 56
    }, {
        img: "https://gxxc-1258879759.cos.ap-guangzhou.myqcloud.com/tree/utils/1543911162735.jpeg",
        name: "跳动心脏长出藤蔓",
        money: 87
    }, {
        img: "https://gxxc-1258879759.cos.ap-guangzhou.myqcloud.com/tree/utils/1543911162735.jpeg",
        name: "经受住了平凡",
        money: 59
    }, {
        img: 'https://gxxc-1258879759.cos.ap-guangzhou.myqcloud.com/tree/utils/1543911164205.jpeg',
        name: "say it's a new world",
        money: 67
    }, {
        img: "https://gxxc-1258879759.cos.ap-guangzhou.myqcloud.com/tree/utils/1543911164317.jpeg",
        name: "不畏高飞 天净沙",
        money: 89
    }
    ]
  },
  pointTimer: '',
  fightTimer: '',
  pkTimer: '',
  addTimer: '',

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var aiList = [];
    var loginName = wx.getStorageSync('loginName');
    var touxiang = wx.getStorageSync('touxiang');
    var money = app.globalData.money;
    that.setData({
      loginName,
      money,
      touxiang,
    })
    http.matching().then( (res) => {
      for( var i = 0; i<6; i++) {
        res.list[i].realName = unescape(res.list[i].realName);
        aiList.push(res.list[i]);
        aiList[i].money = Math.floor(aiList[i].money / 10000 *100)/100;
      }
      util.log(JSON.parse(res.RobotStr));
      var aiPkPlayer = JSON.parse(res.RobotStr);
      aiPkPlayer.money = Math.floor(aiPkPlayer.money/10000 * 100)/100;
      wx.setStorageSync('aiPkPlayer', aiPkPlayer)
      wx.setStorageSync('question', res.question)
      wx.setStorageSync('resultId', res.resultId)
      // util.log(JSON.parse(res.RobotStr));
      // util.log(aiPkPlayer);

      that.setData({
        // aiList,
        aiPkPlayer,
      })
    var peopleList = that.data.peopleList;
    var count = 0;
    util.log(aiList);
    var addTimer = setInterval(() => {
      peopleList.push(aiList[count]);
      that.setData({
        peopleList,
      })
      count ++;
      if(aiList.length-1 < count) {
        var pkTimer = setInterval( () => {
            that.setData({
              matchingShow: false,
              pkShow: true,
            })
        },1000)
        that.pkTimer = pkTimer;
        var fightTimer = setInterval( () => {
            wx.navigateTo({
              url: '/pages/fighting/fighting'
            })
        },2000)
        that.fightTimer = fightTimer;
        clearInterval(addTimer);
      }
    },200)
    that.addTimer = addTimer;
    })
//三个小点定时器
    var point = 0;
    var pointTimer = setInterval(() => {
      point++;
      if(point > 3) {
        point = 1;
      }
      that.setData({
        point,
      })
    },500)
    that.pointTimer = pointTimer;
  },


  back:function () {
    wx.navigateBack({
      delta: 1000
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this;
    clearInterval(that.pointTimer);
    clearInterval(that.pkTimer);
    clearInterval(that.fightTimer);
    clearInterval(that.addTimer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    clearInterval(that.pointTimer);
    clearInterval(that.pkTimer);
    clearInterval(that.fightTimer);
    clearInterval(that.addTimer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})